// ignore_for_file: must_be_immutable

/*
 * @Author: CodeHare
 * @Date: 2023-10-07 10:46:12
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-21 14:59:43
 * @Description: 充电桩功率图
 */

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_echarts/flutter_echarts.dart';
import 'package:get/get.dart';
import 'package:yt_devops/resources/str_res_keys.dart';

import '../../../../../../components/log_utils.dart';

class ChargingStationPowerLineChart extends StatefulWidget {
  final List<dynamic>? powerData;
  final List<dynamic>? powerUseRateData;
  final List<dynamic>? timeList;
  const ChargingStationPowerLineChart(
      {super.key,
      required this.powerData,
      required this.powerUseRateData,
      required this.timeList});

  @override
  State<ChargingStationPowerLineChart> createState() =>
      _ChargingStationPowerLineChartState();
}

class _ChargingStationPowerLineChartState
    extends State<ChargingStationPowerLineChart> {
  @override
  Widget build(BuildContext context) {
    Log.d('ChargingStationPowerLineChart==' + widget.powerData.toString());
    return Echarts(
      captureHorizontalGestures: true,
      option: '''
  {
  /// 提示框
  tooltip: {
    trigger: 'axis',
    backgroundColor: 'rgba(0,0,0,0.85)',
    borderWidth: 0,
    opacity: 0.2,
    confine: true,
    padding: [
      10, // 上
      8, // 右
      10, // 下
      8 // 左
    ],
    textStyle: {
      color: 'white'
    },
    axisPointer: {
      /// 坐标轴指示器配置项
      lineStyle: {
        color: 'rgba(0,0,0,0.09)',
        position: function (pt) {
          return [pt[0], 500];
        }
      }
    },

  },
  /// 坐标轴
  grid: {
    containLabel: true,
    top: '3%',
    left: '5%',
    right: '5%',
    bottom: '10%'
  },
  /// 横轴
  xAxis: {
    type: 'category',
    data: ${jsonEncode(widget.timeList)},  
          splitLine: {
        lineStyle: {
          type: 'dashed', //虚线
        },
      },             
    axisLine:{
      show: false,
    }, 
    axisTick: {
      show: false
    }, 
    axisLabel: {
      color: '#999999',
      fontSize: 11,
      lineStyle: {
          type: 'dashed', //虚线
        },
      interval: function (index) { // 自定义间隔函数 
          if (index % 4 === 0) { // 每四小时显示一次  // 返回值是五分钟为一个间隔
              return true;
          } else {
              return false;
          }
      },
    },
  },
  yAxis: [
    {
  
      alignTicks: true,
      type: 'value',
            max:function (value) {
             return (typeof value.max === 'number' && isNaN(value.max))?1:null;
        },
       min:function (value) {
             return (typeof value.min === 'number' && isNaN(value.min))?0:null;
        },
        // max:100,
        // min: 0,
      // offset: 4,
        splitLine: {
        lineStyle: {
          type: 'dashed', //虚线
        },
      },
      axisLabel: {
        color: '#999999',
        fontSize: 11
      },
    }
  ],
    legend: {
    show: true,
    icon: 'rect',
    bottom:5,
    itemHeight: 3,
    itemWidth: 12,
     textStyle: {
      borderRadius: [2, 2, 2, 2]
    },
    padding: [
    0,  // 上
    0, // 右
    0,  // 下
    0, // 左
]
  },

  /// 图表数据
  series: [
    
     {
      name: ${jsonEncode(ResKey.deviceDischargePower.tr)},
      type: 'line',
      data: ${jsonEncode(widget.powerData)}      
    },
    {
      name: ${jsonEncode(ResKey.devicePowerUtilizationRate.tr)}, // 功率利用率
      type: 'line',
       data: ${jsonEncode(widget.powerUseRateData)}  
    },
  ]
} 
  ''',
    );
  }
}
